package com.asiainfo.rt.compiler;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Vector;

public class JavacRunner {
	public static void main(String[] args) throws NoSuchMethodException,
			InvocationTargetException, IllegalAccessException,
			ClassNotFoundException, IOException {
		if (!JavacResourcesReader.dumpPatterns()) {
			return;
		}

		String versionString = args[0];
		Class<?> aClass = Class.forName(args[1]);

		Method mainMethod = aClass.getMethod("main",
				new Class[] { new String[0].getClass() });
		String[] newArgs;
		if (versionString.indexOf("1.1") > -1) {
			Vector<String> arguments = new Vector<String>();
			boolean isClasspath = false;
			for (int idx = 3; idx < args.length; idx++) {
				String arg = args[idx];
				if ((arg.startsWith("@")) && (!isClasspath)) {
					String path = arg.substring(1);
					addFilesToCompile(arguments, path);
				} else {
					isClasspath = ("-classpath".equals(arg))
							|| ("-cp".equals(arg))
							|| ("-bootclasspath".equals(arg));
					arguments.addElement(arg);
				}
			}
			newArgs = new String[arguments.size()];
			for (int idx = 0; idx < newArgs.length; idx++)
				newArgs[idx] = ((String) arguments.elementAt(idx));
		} else {
			newArgs = new String[args.length - 2];
			System.arraycopy(args, 2, newArgs, 0, newArgs.length);
		}
		expandClasspath(newArgs);
		try {
			mainMethod.invoke(null, new Object[] { newArgs });
		} catch (Throwable e) {
			System.err.print(e.getMessage());
			e.printStackTrace(System.err);
		}
	}

	private static void addFilesToCompile(Vector<String> arguments, String path)
			throws IOException {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(new File(path)));
			for (String filePath = reader.readLine(); filePath != null; filePath = reader
					.readLine())
				arguments.addElement(filePath.replace('/', File.separatorChar));
		} finally {
			if (reader != null)
				reader.close();
		}
	}

	private static void expandClasspath(String[] args) throws IOException {
		for (int idx = 0; idx < args.length; idx++) {
			String arg = args[idx];

			if (("-classpath".equals(arg)) || ("-cp".equals(arg))
					|| ("-bootclasspath".equals(arg))) {
				String cpValue = args[(idx + 1)];
				if (cpValue.startsWith("@"))
					args[(idx + 1)] = readClasspath(cpValue.substring(1));
			}
		}
	}

	public static String readClasspath(String filePath) throws IOException {
		DataInputStream in = new DataInputStream(new BufferedInputStream(
				new FileInputStream(new File(filePath))));
		try {
			String str = readString(in);
			return str;
		} finally {
			in.close();
		}

	}

	private static String readString(DataInput stream) throws IOException {
		int length = stream.readInt();
		if (length == -1)
			return null;

		char[] chars = new char[length];
		byte[] bytes = new byte[length * 2];
		stream.readFully(bytes);

		int i = 0;
		for (int i2 = 0; i < length; i2 += 2) {
			chars[i] = (char) ((bytes[i2] << 8) + (bytes[(i2 + 1)] & 0xFF));
			i++;
		}

		return new String(chars);
	}

	private JavacRunner() {
	}
}